package com.chat.chatting.service.impl;

import com.chat.chatting.dao.FriendDao;
import com.chat.chatting.dto.UserInfoDto;
import com.chat.chatting.entity.FriendEntity;
import com.chat.chatting.entity.UserEntity;
import com.chat.chatting.service.FriendService;
import com.chat.chatting.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (Friend)表服务实现类
 */
@Service("friendService")
public class FriendServiceImpl implements FriendService {
    @Resource
    private FriendDao friendDao;
    @Resource
    private UserService userService;


//    @Override
//    public List<FriendEntity> getAll(Integer userID) {
//        return friendDao.getAll(userID);
//    }

    @Override
    public void create(Integer selfID, Integer friendID) {
        FriendEntity entity = new FriendEntity();
        entity.setSelfID(selfID);
        UserEntity friend = userService.queryByUserID(friendID);
        entity.setFriendID(friendID);
        entity.setFriendPhoto(friend.getPhoto());
        entity.setRemarkFriendName(friend.getNickName());
        friendDao.insert(entity);
    }

    @Override
    public boolean isFriend(Integer selfID, Integer friendID) {
        FriendEntity entity = friendDao.findFriendInfo(selfID, friendID);
        if (entity == null) return false;
        return true;
    }

    @Override
    public UserInfoDto getUserInfo(Integer selfID, Integer friendID) {
        UserInfoDto dto = new UserInfoDto();
        dto.setFlag(isFriend(selfID, friendID));
        UserEntity entity = userService.queryByUserID(friendID);
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }

    @Override
    public void updatePhotos(Integer userID, String photo) {
        friendDao.updatePhotos(userID, photo);
    }
}
